﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO.WaiGaoQiao;
using System.Data;

namespace Model.WaiGaoQiao
{
   public class TbtSporadicApp
    {
        public TbtSporadicApp(bool isgetcount)
        {
            Dais = new DataAccessTbtSporadicApp();
            if (isgetcount)
            {
                sundriescount = Dais.GetRowCount("");
            }
        }

        public TbtSporadicApp()
        { }

        /// <summary>
        /// 自增ID主键
        /// </summary>
        public int ID
        {
            get;
            set;
        }
        /// <summary>
        /// 申领日期
        /// </summary>
        public DateTime AppDt
        {
            get;
            set;
        }
        /// <summary>
        /// 类型
        /// </summary>
        public string SpType
        {
            get;
            set;
        }
        /// <summary>
        /// 零星物品
        /// </summary>
        public int Goods_ID
        {
            get;
            set;
        }
        /// <summary>
        /// 数量
        /// </summary>
        public decimal Count
        {
            get;
            set;
        }
        /// <summary>
        /// 使用人
        /// </summary>
        public string UserMan
        {
            get;
            set;
        }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark
        {
            get;
            set;
        }
        /// <summary>
        /// 记录时间
        /// </summary>
        public DateTime RecordDateTime
        {
            get;
            set;
        }
        /// <summary>
        /// 操作人
        /// </summary>
        public string OperatorID
        {
            get;
            set;
        }
        /// <summary>
        /// 批号
        /// </summary>
        public string OrderNo
        {
            get;
            set;
        }

        private DataAccessTbtSporadicApp Dais;
        private int sundriescount;
        public int _SundriesCount
        {
            get { return sundriescount; }
        }

        /// <summary>
        /// 保存零星物品申领
        /// </summary>
        /// <param name="igpara"></param>
        /// <returns></returns>
        public int SaveSundries(TbtSporadicApp insp)
        {
            object[] sspara = new object[] { insp.ID,insp.AppDt,insp.SpType,insp.Goods_ID, insp.Count, insp.UserMan,
                                             insp.Remark,insp.RecordDateTime,insp.OperatorID,insp.OrderNo };
            return Dais.SaveSundries(sspara);            
        }

        /// <summary>
        /// 获取一条零星物品申领信息
        /// </summary>
        /// <param name="ID">主键</param>
        /// <returns>零星物品申领信息</returns>
        public TbtSporadicApp GetInstockGroup(int ID)
        {
            DataRow item = Dais.GetInstockGroup(ID);
            TbtSporadicApp inspinfo = new TbtSporadicApp()
                {
                    ID = Convert.ToInt32(item[0]),
                    AppDt = Convert.ToDateTime(item[1]),
                    SpType = item[2].ToString(),
                    Goods_ID = Convert.ToInt32(item[3]),
                    Count = Convert.ToDecimal(item[4]),
                    UserMan = item[5].ToString(),
                    Remark = item[6].ToString(),
                    RecordDateTime = Convert.ToDateTime(item[7]),
                    OperatorID = item[8].ToString(),
                    OrderNo = item[9].ToString()
                };
            return inspinfo;
        }

        /// <summary>
        /// 获取零星物品申领信息
        /// </summary>
        /// <param name="pagesize">页大小</param>
        /// <param name="startindex">起始页</param>
        /// <param name="dict">查询条件</param>
        /// <returns>零星物品申领信息</returns>
        public IList<TbtSporadicApp> GetInstockGroup(int pagesize, int startindex, string condition)
        {
            DataSet ds = Dais.GetInstockGroup(pagesize, startindex, condition);
            IList<TbtSporadicApp> insplist = new List<TbtSporadicApp>();
            TbtSporadicApp inspinfo;
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                inspinfo = new TbtSporadicApp()
                {
                    ID = Convert.ToInt32(item[0]),
                    AppDt = Convert.ToDateTime(item[1]),
                    SpType = item[2].ToString(),
                    Goods_ID = Convert.ToInt32(item[3]),
                    Count = Convert.ToDecimal(item[4]),
                    UserMan = item[5].ToString(),
                    Remark = item[6].ToString(),
                    RecordDateTime = Convert.ToDateTime(item[7]),
                    OperatorID = item[8].ToString(),
                    OrderNo = item[9].ToString()
                };
                insplist.Add(inspinfo);
            }
            return insplist;
        }

        /// <summary>
        /// 获取零星物品申领信息(不分页）
        /// </summary>
        /// <param name="dict">查询条件</param>
        /// <returns>零星物品申领信息</returns>
        public IList<TbtSporadicApp> GetInstockGroup(string condition)
        {
            DataSet ds = Dais.GetInstockGroup(condition);
            IList<TbtSporadicApp> insplist = new List<TbtSporadicApp>();
            TbtSporadicApp inspinfo;
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                inspinfo = new TbtSporadicApp()
                {
                    ID = Convert.ToInt32(item[0]),
                    AppDt = Convert.ToDateTime(item[1]),
                    SpType = item[2].ToString(),
                    Goods_ID = Convert.ToInt32(item[3]),
                    Count = Convert.ToDecimal(item[4]),
                    UserMan = item[5].ToString(),
                    Remark = item[6].ToString(),
                    RecordDateTime = Convert.ToDateTime(item[7]),
                    OperatorID = item[8].ToString(),
                    OrderNo = item[9].ToString()
                };
                insplist.Add(inspinfo);
            }
            return insplist;
        }

        /// <summary>
        /// 删除零星物品申领
        /// </summary>
        /// <param name="ID">主键</param>
        /// <returns></returns>
        public int DeleteInstockGroup(string id)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string[] temp = id.Split(',');
                if (temp.Length == 1)
                {
                    sb.AppendFormat(" ID={0} ", temp[0]);
                    return Dais.DeleteInstockGroup(sb.ToString());
                }
                else
                {
                    for (int i = 0; i < temp.Length; i++)
                    {
                        sb.AppendFormat("  ID={0} OR", temp[i]);
                    }

                    return Dais.DeleteInstockGroup(sb.ToString().Substring(0, sb.ToString().Length - 2));

                }



            }
            catch
            {

                throw;
            }
         
        }

        /// <summary>
        /// 获取零星物品的数据
        /// </summary>
        /// <returns></returns>
        public int GetRowCount(string condition )
        {
            return Dais.GetRowCount(condition);
        }
          /// <summary>
        /// 获取批号
        /// </summary>
        /// <returns></returns>
        public string GetInstockSporadicOrderNo()
        {
            return Dais.GetInstockSporadicOrderNo();
        }
    }
}
